package com.bootdo.lvyou.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.lvyou.domain.LvyouScenicSpot;
import com.bootdo.lvyou.service.LvyouScenicMarkerService;
import com.bootdo.lvyou.service.LvyouScenicSpotService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.LvyouScenicRoute;
import com.bootdo.lvyou.service.LvyouScenicRouteService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 景点路线
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-09 16:35:39
 */
 
@Controller
@RequestMapping("/lvyou/lvyouScenicRoute")
public class LvyouScenicRouteController extends BaseController{


	@Autowired
	private LvyouScenicRouteService lvyouScenicRouteService;

	@Autowired
	private LvyouScenicSpotService lvyouScenicSpotService;

	@Autowired
	private LvyouScenicMarkerService lvyouScenicMarkerService;

	@ModelAttribute
	public LvyouScenicRoute get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouScenicRouteService.get(id);
		}else{
			return new LvyouScenicRoute();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/lvyouScenicRoute','lvyou:lvyouScenicRoute:lvyouScenicRoute',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:lvyouScenicRoute:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:lvyouScenicRoute:lvyouScenicRoute")	
	String LvyouScenicRoute(LvyouScenicRoute lvyouScenicRoute,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicRoute/lvyouScenicRoute";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouScenicRoute:lvyouScenicRoute")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        if(!isAdmin()){
        	query.put("userId", getUserId());
		}
		List<LvyouScenicRoute> lvyouScenicRouteList = lvyouScenicRouteService.list(query);
		int total = lvyouScenicRouteService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouScenicRouteList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou:lvyouScenicRoute:edit")
	String add(LvyouScenicRoute lvyouScenicRoute,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicRoute/lvyouScenicRouteedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouScenicRoute lvyouScenicRoute) {
		return R.ok().put("i",lvyouScenicRoute);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:lvyouScenicRoute:edit")
	public R save( LvyouScenicRoute lvyouScenicRoute){
		if(null != lvyouScenicRoute.getSpotId()){
			LvyouScenicSpot lvyouScenicSpot  =lvyouScenicSpotService.get(lvyouScenicRoute.getSpotId());
			if(null != lvyouScenicSpot){
				lvyouScenicRoute.setSpotName(lvyouScenicSpot.getName());
			}
		}
		if(!isAdmin()){
			lvyouScenicRoute.setUserId(getUserId());
			lvyouScenicRoute.setUserName(getUsername());
		}
		if(lvyouScenicRouteService.save(lvyouScenicRoute)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicRoute:edit")
	public R remove( Long id){
		if(lvyouScenicRouteService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicRoute:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouScenicRouteService.batchRemove(ids);
		return R.ok();
	}
	
}